{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python38664bite0826404ec9f4f88a00ca1565c140931",
   "display_name": "Python 3.8.6 64-bit"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "函数调用的正确姿势和错误姿势"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def foo(a:int, b = 1, c = 'Yes', d = True)-> str:\n",
    "    print('参数a：{}'.format(a))\n",
    "    if b != 1:\n",
    "        print('参数b：{}, 默认值为1，本函数中的b为用户输入'.format(b))\n",
    "    else:\n",
    "        print('参数b：默认值为1，用户未输入参数')\n",
    "\n",
    "    if c != 'Yes':\n",
    "        print('参数c：{}, 默认值为Yes，本函数中的c为用户输入'.format(b))\n",
    "    else:\n",
    "        print('参数c：默认值为Yes，用户未输入参数')\n",
    "    if d != True:\n",
    "        print('参数d：{}, 默认值为True，本函数中的d为用户输入'.format(b))\n",
    "    else:\n",
    "        print('参数d：默认值为True，用户未输入参数')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "参数a：1\n参数b：默认值为1，用户未输入参数\n参数c：默认值为Yes，用户未输入参数\n参数d：默认值为True，用户未输入参数\n"
    }
   ],
   "source": [
    "foo(1) # 按位置参数传入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "参数a：1\n参数b：2, 默认值为1，本函数中的b为用户输入\n参数c：2, 默认值为Yes，本函数中的c为用户输入\n参数d：2, 默认值为True，本函数中的d为用户输入\n"
    }
   ],
   "source": [
    "foo(a = 1 ,b = 2, c = 'No', d = False) # 按位置和关键字传入，位置在前，关键字在后"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "参数a：3\n参数b：2, 默认值为1，本函数中的b为用户输入\n参数c：2, 默认值为Yes，本函数中的c为用户输入\n参数d：默认值为True，用户未输入参数\n"
    }
   ],
   "source": [
    "foo(b = 2, c = 'NONONONO', a = 3) # 关键字可以不按顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "参数a：5\n参数b：2, 默认值为1，本函数中的b为用户输入\n参数c：默认值为Yes，用户未输入参数\n参数d：默认值为True，用户未输入参数\n"
    }
   ],
   "source": [
    "foo(5, b = 2) # 同上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "参数a：3\n参数b：3, 默认值为1，本函数中的b为用户输入\n参数c：3, 默认值为Yes，本函数中的c为用户输入\n参数d：3, 默认值为True，本函数中的d为用户输入\n"
    }
   ],
   "source": [
    "foo(3, 3, \"no\", False) # 对多个变量按位置传入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "TypeError",
     "evalue": "foo() missing 1 required positional argument: &#39;a&#39;",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m&lt;ipython-input-23-c19b6d9633cf&gt;\u001b[0m in \u001b[0;36m&lt;module&gt;\u001b[0;34m\u001b[0m\n\u001b[0;32m----&gt; 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: foo() missing 1 required positional argument: &#39;a&#39;"
     ]
    }
   ],
   "source": [
    "foo() # 错误1: 不传入参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "TypeError",
     "evalue": "foo() got multiple values for argument &#39;a&#39;",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m&lt;ipython-input-24-3ebc238fcbcd&gt;\u001b[0m in \u001b[0;36m&lt;module&gt;\u001b[0;34m\u001b[0m\n\u001b[0;32m----&gt; 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: foo() got multiple values for argument &#39;a&#39;"
     ]
    }
   ],
   "source": [
    "foo(1, a=2) # 错误2:位置参数 和 关键字参数 传入重复"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (&lt;ipython-input-25-30f0ecc9538a&gt;, line 1)",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m&quot;&lt;ipython-input-25-30f0ecc9538a&gt;&quot;\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m    foo(b = 3, 3)\u001b[0m\n\u001b[0m               ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "foo(b = 3, 3) # 错误3:关键字参数放在了位置参数前面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "TypeError",
     "evalue": "foo() got an unexpected keyword argument &#39;e&#39;",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m&lt;ipython-input-26-ac2a3769bc08&gt;\u001b[0m in \u001b[0;36m&lt;module&gt;\u001b[0;34m\u001b[0m\n\u001b[0;32m----&gt; 1\u001b[0;31m \u001b[0mfoo\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: foo() got an unexpected keyword argument &#39;e&#39;"
     ]
    }
   ],
   "source": [
    "foo(e = 0) # 错误4：对不存在的变量传入了参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}